package com.ruoyi.project.module.ordersDetail.controller;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.ErpEnums;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UserUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.module.products.service.IProductsService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.dict.domain.DictData;
import com.ruoyi.project.system.dict.service.IDictDataService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.ordersDetail.domain.OrdersDetail;
import com.ruoyi.project.module.ordersDetail.service.IOrdersDetailService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

/**
  * @创建人    cbx
  * @创建时间  2018/9/13 0013 下午 3:20
  * @描述
  */  
@Controller
@RequestMapping("/module/shippedOrder")
public class ShippedOrderController extends BaseController {
    private String prefix = "module/ordersDetail";

    @Autowired
    private IOrdersDetailService ordersDetailService;

    @Autowired
    private IProductsService iProductsService;
    @Autowired
    private IDeptService deptService;


    @RequiresPermissions("module:shippedOrder:view")
    @GetMapping()
    public String ordersDetail(ModelMap mmap) {
        mmap.put("depList", deptService.selectDeptAll());
        return prefix + "/shippedOrder";
    }

    /**
     * 查询订单详情列表
     */
    @RequiresPermissions("module:shippedOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrdersDetail ordersDetail) {
        ordersDetail.setOrderStatus("10703");
        ordersDetail.setIsDel(0);
        startPageChange();

        if("".equals(ordersDetail.getCompanyID())||ordersDetail.getCompanyID()==null){
            ordersDetail.setCompanyID(UserUtils.getDepId());
        }else if("100".equals(ordersDetail.getCompanyID())){
            ordersDetail.setCompanyID("");
        }

        //根据公司id查询订单
        List<OrdersDetail> list = ordersDetailService.selectOrdersDetailList(ordersDetail);

        List<OrdersDetail> sumCountList = ordersDetailService.selectSumCountOrdersDetailList(ordersDetail);

        return getDataTableAsSum(list, sumCountList);
    }





    @RequiresPermissions("module:shippedOrder:returnReceived")
    @Log(title = "退货", action = BusinessType.UPDATE)
    @PostMapping("/returnReceived")
    @ResponseBody
    public List<Integer> sendOut(OrdersDetail ordersDetail) {
        String updateBy = ShiroUtils.getLoginName();

        String id = ordersDetail.getId();
        ordersDetail = ordersDetailService.selectOrdersDetailById(id);
        ordersDetail.setUpdateBy(updateBy);
        //现在的状态改为上一状态
        ordersDetail.setLastStatus(ordersDetail.getOrderStatus());
        //修改后的状态
        ordersDetail.setOrderStatus(ErpEnums.OrderStatus.Return_Received.getValue().toString());


        //--------------------------修改商品库存信息-------------------------
        ArrayList<String> skuList = new ArrayList<String>();//sku集合
        ArrayList<Integer> numList = new ArrayList<>();//发货数量集合
        skuList.add(ordersDetail.getUnitSKUs());
        numList.add(ordersDetail.getOfItems());
        //退货增加库存数量
        List<Integer> refundPutInQuantity = iProductsService.uploadProductAsMainSkuAndType(skuList, numList, "RefundPutInQuantity");

//        if(SoldQuantity>0){
            ordersDetailService.updateOrdersDetail(ordersDetail);//退货
//        }

        return refundPutInQuantity;
    }



}
